/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package aiaudio.processing.ratings;

import aiaudio.processing.ratings.normilize.InitialData;
import aiaudio.lastfm.hbase.CannotCreateTableException;
import aiaudio.processing.MainTableGroup;
import aiaudio.processing.base.TimeMeasuredAlgorithm;
import aiaudio.processing.ratings.inverse.InverseListenCountAlgorithm;
import aiaudio.processing.ratings.normilize.RatingAlgorithmDefinition;
import aiaudio.processing.ratings.normilize.RatingRecalculatorBase;
import java.io.IOException;

/**
 *
 * @author nastya
 */
public class PreprocessRatingsAlgorithm extends TimeMeasuredAlgorithm<PreprocessRatingParameters> {

    private RatingRecalculatorBase normilizationTask = null;

    public PreprocessRatingsAlgorithm(MainTableGroup mainTableGroup) {
        super(mainTableGroup, "preprocess ratings");
    }


    @Override
    protected void createTemporaryTables() throws IOException {
    }

    @Override
    protected void measureAll() throws IOException {
    }

    @Override
    protected void removeTemporaryTables() {
    }

    @Override
    protected void writeParameters(PreprocessRatingParameters parameters) throws IOException {
    }

    @Override
    protected void process() throws IOException, CannotCreateTableException, InterruptedException, ClassNotFoundException {
        if (parameters.getInitialData() == InitialData.INVERSED_LISTEN_COUNT) {
            createInversedRatings();
        }

        createNormilizationTask();
    }

    private void createNormilizationTask() throws CannotCreateTableException, ClassNotFoundException, InterruptedException, IOException {
        RatingAlgorithmDefinition def = RatingAlgorithmDefinition.createDefinition(
                parameters.getInitialData(), parameters.getNormalizaton(), parameters.getDefaultValue(), mainTableGroup);
        normilizationTask = new RatingRecalculatorBase(mainTableGroup, def);
        normilizationTask.prepeare();
        normilizationTask.start();
    }

    private void createInversedRatings() throws IOException, InterruptedException, ClassNotFoundException, CannotCreateTableException {
        InverseListenCountAlgorithm alg = new InverseListenCountAlgorithm(mainTableGroup);
        alg.start();
    }
}
